package interfacePackage;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;

import mapPackage.MapException;
import mapPackage.Route;
import mapPackage.typeRoute.SAutoroute;
import mapPackage.typeRoute.SDepartementale;
import mapPackage.typeRoute.SNationale;
import mapPackage.typeRoute.SRouteIndeterminee;
import mapPackage.typeRoute.STypeRoute;
import xmlPackage.Carte;

public class PanelPopupAjouterRoute extends JPanel implements ActionListener {
	
	public static String CHOIX_VALIDER= "CHOIX_VALIDER";  //  @jve:decl-index=0:
	public static String CHOIX_ANNULER= "CHOIX_ANNULER";  //  @jve:decl-index=0:
	
	private static PanelPopupAjouterRoute m_instance;
	private static final long serialVersionUID = 1L;
	private JLabel jLabelAjouterRoute = null;
	private JButton jButtonAjouter = null;
	private JPanel jPanelAjouterRouteInterieur = null;
	private JLabel jLabelNomRoute = null;
	private JLabel jLabelTypeRoute = null;
	private JTextField jTextFieldNomRoute = null;
	private JComboBox jComboBoxTypeRoute = null;

	private JButton jButtonAnnuler = null;

	/**
	 * This is the default constructor
	 */
	public PanelPopupAjouterRoute() {
		super();
		initialize();
	}

	public static PanelPopupAjouterRoute getInstance() {
		if (null == m_instance) { // Premier appel
			m_instance = new PanelPopupAjouterRoute();
        }
        return m_instance;
	}
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabelAjouterRoute = new JLabel();
		jLabelAjouterRoute.setBounds(new Rectangle(104, 9, 159, 20));
		jLabelAjouterRoute.setText("Ajouter une route");
		jLabelAjouterRoute.setFont(new Font("Tahoma", Font.BOLD, 16));
		this.setSize(370, 301);
		this.setLayout(null);
		this.add(jLabelAjouterRoute, null);
		this.add(getJButtonAjouter(), null);
		this.add(getJPanelAjouterRouteInterieur(), null);
		this.setFont(new Font("Dialog", Font.PLAIN, 18));
		this.setBackground(new Color(163, 163, 224));
		this.add(getJButtonAnnuler(), null);
		
	}

	/**
	 * This method initializes jButtonAjouter	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAjouter() {
		if (jButtonAjouter == null) {
			jButtonAjouter = new JButton();
			jButtonAjouter.setBounds(new Rectangle(65, 265, 105, 19));
			jButtonAjouter.setFont(new Font("Tahoma", 1, 11));
			jButtonAjouter.setForeground(new Color(38, 38, 115));
			jButtonAjouter.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jButtonAjouter.setText("Ajouter");
			jButtonAjouter.setBackground(new Color(209, 209, 240));
			jButtonAjouter.addActionListener(this);
			jButtonAjouter.setActionCommand(CHOIX_VALIDER);
			
		}
		return jButtonAjouter;
	}

	/**
	 * This method initializes jPanelAjouterRouteInterieur	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanelAjouterRouteInterieur() {
		if (jPanelAjouterRouteInterieur == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 2;
			gridBagConstraints3.gridy = 3;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints21.gridy = 1;
			gridBagConstraints21.weightx = 1.0;
			gridBagConstraints21.gridx = 2;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints11.gridy = 0;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.gridwidth = 2;
			gridBagConstraints11.gridx = 1;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridheight = 2;
			gridBagConstraints2.gridy = 2;
			
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.gridx = 0;
			gridBagConstraints1.gridy = 1;
			jLabelTypeRoute = new JLabel();
			jLabelTypeRoute.setText("Type de route");
			jLabelTypeRoute.setHorizontalAlignment(SwingConstants.CENTER);
			jLabelTypeRoute.setPreferredSize(new Dimension(100, 30));
			
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jLabelNomRoute = new JLabel();
			jLabelNomRoute.setText("Nom de la route :");
			jLabelNomRoute.setPreferredSize(new Dimension(100, 30));
			jLabelNomRoute.setHorizontalAlignment(SwingConstants.CENTER);
			jPanelAjouterRouteInterieur = new JPanel();
			jPanelAjouterRouteInterieur.setLayout(new GridBagLayout());
			jPanelAjouterRouteInterieur.setBounds(new Rectangle(16, 42, 341, 213));
			jPanelAjouterRouteInterieur.setBackground(new Color(209, 209, 240));
			jPanelAjouterRouteInterieur.setBorder(BorderFactory.createLineBorder(new Color(38, 38, 115), 2));
			jPanelAjouterRouteInterieur.add(jLabelNomRoute, gridBagConstraints);
			jPanelAjouterRouteInterieur.add(jLabelTypeRoute, gridBagConstraints1);
			jPanelAjouterRouteInterieur.add(getJTextFieldNomRoute(), gridBagConstraints11);
			jPanelAjouterRouteInterieur.add(getJComboBoxTypeRoute(), gridBagConstraints21);
		}
		return jPanelAjouterRouteInterieur;
	}

	/**
	 * This method initializes jTextFieldNomRoute	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldNomRoute() {
		if (jTextFieldNomRoute == null) {
			jTextFieldNomRoute = new JTextField();
			jTextFieldNomRoute.setPreferredSize(new Dimension(200, 20));
		}
		return jTextFieldNomRoute;
	}

	/**
	 * This method initializes jComboBoxTypeRoute	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getJComboBoxTypeRoute() {
		if (jComboBoxTypeRoute == null) {
			jComboBoxTypeRoute = new JComboBox();
			jComboBoxTypeRoute.setPreferredSize(new Dimension(200, 10));
			jComboBoxTypeRoute.addItem(	SAutoroute.getInstance().getNom());
			jComboBoxTypeRoute.addItem(	SDepartementale.getInstance().getNom());
			jComboBoxTypeRoute.addItem(	SNationale.getInstance().getNom());
			jComboBoxTypeRoute.addItem(	SRouteIndeterminee.getInstance().getNom());			

		}
		return jComboBoxTypeRoute;
	}

	
	public void actionPerformed(ActionEvent e) {
		System.out.println("Event au niveau de PanelPopupAjouterRoute : " + e.getActionCommand());
		
		TobtobFenetre fenetrePrincipale = TobtobFenetre.getInstance();
		
		if (PanelPopupAjouterRoute.CHOIX_VALIDER.equals(e.getActionCommand())) {
			//ajout des donnees 

			String w_nomRoute = PanelPopupAjouterRoute.getInstance().getJTextFieldNomRoute().getText();
			if (w_nomRoute.equals("")) {
				JOptionPane.showMessageDialog(this, "Entrez un nom de route avant de valider !", "Entrez un nom de Route", JOptionPane.WARNING_MESSAGE);	
				
			}
			else if (Carte.GetInstance().getRoutes().contains(w_nomRoute)) {
				JOptionPane.showMessageDialog(this, "La route existe deja : Changez le nom !", "Entrez un nom de Route non existant", JOptionPane.WARNING_MESSAGE);		
			}	
			else {
				String w_type = (String)PanelPopupAjouterRoute.getInstance().getJComboBoxTypeRoute().getSelectedItem();
	    	    STypeRoute w_type_route;
	    	    Route eeze = new Route("",SAutoroute.getInstance());
	    	    try {
	    	    if(w_type.equals(SAutoroute.getInstance().getNom()))
						Carte.GetInstance().ajouterRoute(new Route(w_nomRoute,SAutoroute.getInstance()));
	    	    if(w_type.equals(SDepartementale.getInstance().getNom()))
					Carte.GetInstance().ajouterRoute(new Route(w_nomRoute,SDepartementale.getInstance()));
	    	    if(w_type.equals(SNationale.getInstance().getNom()))
					Carte.GetInstance().ajouterRoute(new Route(w_nomRoute,SNationale.getInstance()));
	    	    if(w_type.equals(SRouteIndeterminee.getInstance().getNom()))
					Carte.GetInstance().ajouterRoute(new Route(w_nomRoute,SRouteIndeterminee.getInstance()));
			 // si autres type de route les ajouter ici  
	    	    
	    	    } catch (MapException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
		    //on ferme cette fenetre et on restaure lancienne 
				FramePopupAjouterRoute.getInstance().dispose();
	    		fenetrePrincipale.setEnabled(true);
	    		fenetrePrincipale.setVisible(true);
	    		PanelAdministrationRoutes.getInstance().mettreAjourTableroutes();
	    		fenetrePrincipale.repaint(); 
			
			}
		}
		else if (PanelPopupAjouterRoute.CHOIX_ANNULER.equals(e.getActionCommand())) {
			FramePopupAjouterRoute.getInstance().dispose();
    		fenetrePrincipale.setEnabled(true);
    		fenetrePrincipale.setVisible(true);
    		fenetrePrincipale.repaint(); 
		}
		else {
	    	//RAS
		    }
		
	}

	/**
	 * This method initializes jButtonAnnuler	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonAnnuler() {
		if (jButtonAnnuler == null) {
			jButtonAnnuler = new JButton();
			jButtonAnnuler.setFont(new Font("Tahoma", 1, 11));
			jButtonAnnuler.setForeground(new Color(38, 38, 115));
			jButtonAnnuler.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
			jButtonAnnuler.setText("Annuler");
			jButtonAnnuler.setLocation(new Point(193, 265));
			jButtonAnnuler.setSize(new Dimension(105, 19));
			jButtonAnnuler.setBackground(new Color(209, 209, 240));
			jButtonAnnuler.addActionListener(this);
			jButtonAnnuler.setActionCommand(CHOIX_ANNULER);
		}
		return jButtonAnnuler;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
